package com.nf.health.app.activity.manage_health.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by changpeng on 2017/06/1.
 * 慢性病智慧防控计划 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class PreventionChronicAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD = 0;
    private static final int TYPE_CONTENT = 1;

    private Context mContext;
    private int[] imageViewIds = {R.drawable.prevention_pressure_image,R.drawable.prevention_sugar_image};
    private String[] title = {"高血压智慧防控计划","糖尿病智慧防控计划"};
    public PreventionChronicAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_prevention_chronic_head, parent, false);
                return new HeadHolder(view);
            case TYPE_CONTENT:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_prevention_chronic_content, parent, false);
                return new ContentHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof HeadHolder) {
            HeadHolder headHolder = (HeadHolder) holder;
            headHolder.tvTitle.setText("请问您需要参加哪项慢性病智慧防控计划");
        }

        if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            contentHolder.tvTitle.setText(title[position - 1]);
            contentHolder.image.setImageResource(imageViewIds[position - 1]);
            if (mOnItemClickLitener != null){
                contentHolder.llParent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return title.length + 1;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD;
        }
        return TYPE_CONTENT;
//        return super.getItemViewType(position);
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tvTitle;

        public HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ContentHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.iv_image)
        ImageView image;
        @Bind(R.id.ll_parent)
        LinearLayout llParent;

        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
